
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import net.sourceforge.floggy.persistence.FloggyException;
import net.sourceforge.floggy.persistence.ObjectSet;
import net.sourceforge.floggy.persistence.PersistableManager;

public class Menu extends Canvas {

    private int menux = 0;
    private int menuy = 0;
    private int menuw = getWidth();
    private int menuh;
    private int selected_index = 0;
    public static Game g;
    private static final String[] MENUS = {"Novo Jogo", "Continuar", "Recordes", "Sobre", "Sair"};
    private boolean continuarHabilitado;
    private Font fonte;

    public Menu() {
        setFullScreenMode(true);
        menuh = getHeight();

        PersistableManager pm = PersistableManager.getInstance();
        try {
            ObjectSet result = pm.find(Player.class, null, null);
            if (result.size() < 1) {
                continuarHabilitado = false;
            } else {
                continuarHabilitado = true;
            }
        } catch (FloggyException ex) {
            //TODO Problema ao carregar
        }

        fonte = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN,
                Font.SIZE_SMALL);
    }

    protected void paint(Graphics g) {
        g.setColor(0);
        g.fillRect(0, 0, getWidth(), getHeight());

        String titulo = "DMath";
        int h = g.getFont().getHeight();
        int w = g.getFont().stringWidth(titulo);

        Game.drawBevelRect(g, menux, menuy, menuw, menuh, 0x222222, 0x444444,
                0x666666);

        Game.drawBevelRect(g, menux + 4, menuy + h + 1, menuw - 8, menuh
                - (4 + h + 1), 0x666666, 0x000000, 0x222222);

        g.setColor(0x00ff50);
        g.drawString(titulo, (getWidth() - w) / 2, 1, 0);

        g.setFont(fonte);

        //TODO Continuar desabilitado
        for (int index = 0; index < MENUS.length; index++) {
            boolean habilitado = true;
            if (index == 1) {
                habilitado = continuarHabilitado;
            }
            drawMenuItem(index, habilitado, MENUS[index], g);
        }
    }

    public void drawMenuItem(int index, boolean habilitado, String texto, Graphics g) {
        g.setColor(0x00ff00);
        if (selected_index == index) {
            g.setColor(0xffffff);
        }
        if (!habilitado) {
            g.setColor(0x555555);
        }
        g.drawString(texto, menux
                + ((menuw - g.getFont().stringWidth(texto)) / 2), menuy
                + ((menuw - g.getFont().getHeight()) / 2 + 2)
                + (index * g.getFont().getHeight()), 0);

    }

    protected void keyPressed(int keyCode) {
        String menu;
        switch (getGameAction(keyCode)) {
            case UP:
                selected_index--;

                if (selected_index < 0) {
                    selected_index = MENUS.length - 1;
                }

                menu = MENUS[selected_index];

                if (menu.equals("Continuar") && !continuarHabilitado) {
                    selected_index--;
                }

                repaint();
                break;
            case DOWN:
                selected_index++;

                if (selected_index > MENUS.length - 1) {
                    selected_index = 0;
                }

                menu = MENUS[selected_index];

                if (menu.equals("Continuar") && !continuarHabilitado) {
                    selected_index++;
                }

                repaint();
                break;
            case FIRE:
                menu = MENUS[selected_index];

                if (menu.equals("Novo Jogo")) {
                    g = new Game();
                    DMath.getInstance().getDisplay().setCurrent(g);
                    g.start();
                }

                if (menu.equals("Continuar")) {
                    g = new Game();
                    DMath.getInstance().getDisplay().setCurrent(g);
                    g.continueGame();
                }

                if (menu.equals("Sair")) {
                    DMath.getInstance().destroyApp(true);
                }

                if (menu.equals("Recordes")) {
                    Recordes r = new Recordes();
                    DMath.getInstance().getDisplay().setCurrent(r);
                }

                if (menu.equals("Sobre")) {
                    DMath.getInstance().getDisplay().setCurrent(new Sobre());
                }
                break;
        }
    }

    protected void pointerPressed(int x, int y) {
        int index;
        index = (y - (menuy
                + ((menuw - fonte.getHeight()) / 2 + 2))) / fonte.getHeight();
        System.out.println(index);

        if(index>=MENUS.length || index<0){
            return;
        }
        
        String menu = MENUS[index];

        if (menu.equals("Novo Jogo")) {
            g = new Game();
            DMath.getInstance().getDisplay().setCurrent(g);
            g.start();
        }

        if (menu.equals("Continuar")&& continuarHabilitado) {
            g = new Game();
            DMath.getInstance().getDisplay().setCurrent(g);
            g.continueGame();
        }

        if (menu.equals("Sair")) {
            DMath.getInstance().destroyApp(true);
        }

        if (menu.equals("Recordes")) {
            Recordes r = new Recordes();
            DMath.getInstance().getDisplay().setCurrent(r);
        }

        if (menu.equals("Sobre")) {
            DMath.getInstance().getDisplay().setCurrent(new Sobre());
        }
        
    }

}
